<?php

namespace app\home\home;
use app\admin\controller\Admin;
use app\home\model\User;
use think\Controller;
use think\Db;
use think\exception\DbException;
use think\Loader;
use think\Request;
use app\common\model\AdminAnnex as AnnexModel;
class Comments extends Base
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index(Request $request)
    {

    }

    /**
     * 添加评论
     *
     * @return \think\Response
     */
    public function create(Request $request)
    {
        $data = $request->param();
        $validate = validate('Comments');
        $res = $validate->scene('add')->check($data);
        if (!$res){
            return apistatus(0,$validate->getError());
        }
        $insert['fromid'] = $data['id'];
        $insert['sqid'] = $data['sqid'];
        $insert['toid'] = $data['toid'];
        $insert['contents'] = $data['contents'];
        $insert['create_time'] = time();
        $res = model('comment')->insert($insert);
        if ($res){
            $news['fromid'] = $data['id'];
            $news['toid'] = $data['toid'];
            $news['news_content'] = '评论了您的动态';
            $news['create_time'] = time();
            $news['type'] = 3;
            $news['sqid'] = $insert['sqid'];
            \db('home_news')->insert($news);
            return apistatus(1,'评论成功');
        }else{
            return apistatus(0,'评价失败');
        }
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete(Request $request)
    {
        //
        $data = $request->param();
        $uid = $data['uid'];
        $cid = $data['cid'];
        $res  = model('comment')->where(['id'=>$cid,'fromid'=>$uid])->delete();
        if ($res){
            return apistatus(1,'删除成功');
        }else{
            return apistatus(0,'失败，请重试');
        }
    }
    /**
     * 获取指定内容的所有评价
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function getCommentsByid(Request $request)
    {
        $data = $request->param();
        if (empty($data['sqid'])){
            return apistatus(0,'缺少请求数据');
        }
        $page = isset($data['page']) ? $data['page'] : 1;
        $listrow = isset($data['listrow']) ? $data['listrow'] : 1;
        $list = \db('home_comments')->alias('C')
                 ->join('home_users U','C.fromid=U.id')
                 ->order('C.create_time desc')
                 ->where(['C.sqid'=>$data['sqid']])
                 ->field('C.id,C.create_time,C.contents,U.headicon,U.nickname,U.address')
                 ->paginate($listrow,true,['page'=>$page]);

        if (!empty($list)){
            return apistatus(1,'评论获取成功',$list);
        }else{
            return apistatus(0,'无数据');
        }
    }




}
